#include "prs_tcp_client.h"

prs_tcp_client::prs_tcp_client()
{
	addr = {};
	sListen = {};
	Connection = {};
	WSAData wsaData;
	WORD DllVersion = MAKEWORD(2, 1);
	if (WSAStartup(DllVersion, &wsaData) != 0)
	{
		MessageBoxA(NULL, "Winsock startup error", "Error", MB_OK | MB_ICONERROR);
	}
}

prs_tcp_client::~prs_tcp_client()
{
	closesocket(Connection);
	WSACleanup();
}

void prs_tcp_client::begin()
{
	addr; //Adres przypisany do socketu Connection
	int sizeofaddr = sizeof(addr);
	addr.sin_addr.s_addr = inet_addr(clientIp.c_str()); //Addres = localhost
	addr.sin_port = htons(clientPort); //Port = 1111
	addr.sin_family = AF_INET; //IPv4 Socket

	Connection = socket(AF_INET, SOCK_STREAM, NULL);
	if (connect(Connection, (SOCKADDR*)&addr, sizeofaddr) != 0) //Connection
	{
		MessageBoxA(NULL, "[Client] Bad Connection", "Error", MB_OK | MB_ICONERROR);
		return;
	}
}

void prs_tcp_client::tx(float_t* txd, size_t length)
{
	send(Connection, (char*)txd, (int)sizeof(float_t) * length, NULL);
}

void prs_tcp_client::rx(float_t* rxd, size_t length)
{
	recv(Connection, (char*)rxd, (int)sizeof(float_t) * length, NULL);
}

void prs_tcp_client::tx(double_t* txd, size_t length)
{
	send(Connection, (char*)txd, (int)sizeof(double_t) * length, NULL);
}

void prs_tcp_client::rx(double_t* rxd, size_t length)
{
	recv(Connection, (char*)rxd, (int)sizeof(double_t) * length, NULL);
}